using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class GameInput : MonoBehaviour
{

    [SerializeField]
    private bool test = false;

    private PlayerInput playerInput;

    public static GameInput instance;

    public event Action<Vector2> onMoveAction;
    public event Action OnJumpAction;

    private void Awake() {
        instance = this;
        playerInput = new PlayerInput();
        playerInput.Enable();

        playerInput.Player.Move.performed += onMove;
        playerInput.Player.Jump.started += OnJump;
    }

    private void onMove(InputAction.CallbackContext context) {
        Vector2 value = context.ReadValue<Vector2>();
        onMoveAction?.Invoke(value);
    }

    private void OnJump(InputAction.CallbackContext context) {
        OnJumpAction?.Invoke();
    }

    private void OnEnable() {
        if(playerInput != null) {
            playerInput.Enable();
        }
    }

    private void OnDisable() {
        if(playerInput != null) {
            playerInput.Disable();
        }
    }

    public Vector3 GetMovementVectorNormolized() {
        Vector3 dir = new Vector3();
    #if ENABLE_INPUT_SYSTEM
        Vector2 v = playerInput.Player.Move.ReadValue<Vector2>();
        dir = new Vector3(v.x,0,0);
    #else
            // if(Input.GetKey(KeyCode.W)) {
            //     dir.z += 1;
            // }
            // if(Input.GetKey(KeyCode.S)) {
            //     dir.z -= 1;
            // }
            if(Input.GetKey(KeyCode.A)) {
                dir.x -= 1;
            }
            if(Input.GetKey(KeyCode.D)) {
                dir.x += 1;
            }
    #endif
        return dir.normalized;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
